//
//  SettingViewController+ClearCache.m
//  JKFuYinProject
//
//  Created by jiukuyinyue on 2017/9/30.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "SettingViewController+ClearCache.h"
#import "LCCatCacheSize.h"
#import "LCDeleteFile.h"
#import "LCFMDB.h"

@implementation SettingViewController (ClearCache)

- (NSString *)readCacheSize {
    if ([LCCatCacheSize readCacheSize] < 2) {
        return [NSString stringWithFormat:@"0.0M"];
    }else{
        return [NSString stringWithFormat:@"%.1fM",  [LCCatCacheSize readCacheSize]];
    }

}
///清除数据库里的纪录缓存
- (void)clearDB {
    LCFMDB *fmdb = [LCFMDB sharedInstance];
    fmdb.dbVersion = 0;
    [fmdb deleteDB];
}

- (void)clearCache {
    //清除数据库里的纪录缓存
    [self clearDB];

    //清除缓存
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                   message:@"确定要清除吗？"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              
                                                              [SVProgressHUD show];
                                                              [SVProgressHUD dismissWithDelay:1];
                                                              
                                                              //删除图片缓存
                                                                  [LCDeleteFile deleteFileWithISHasSuffix:NO];
                                                                  //
                                                                      //网页缓存
                                                                      NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
                                                                      
                                                                      //// Date from
                                                                      
                                                                      NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];

                                                                      //// Execute
                                                                      
                                                                      [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
                                                                          
                                                                          // Done
                                                                      }];
                                                                      //

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      
      MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
      
      hud.mode = MBProgressHUDModeCustomView;
      
      hud.label.text = @"清除成功!";
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            self.huanCunLabel.text = [self readCacheSize];

        });
    });
                                                                 
                                                              
                                                          }];
    NSString *cancelStr = @"取消";
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:cancelStr style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:defaultAction];
    
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}

@end
